<?php
class friendlinkApp extends core
{
	public function listAction($arg)
	{
		$template	= $this->template();
		$list		= base::init('friendlink')->fetchAll(array('wid'=>$arg['wid']),'sort');
		
		$template->assign('list',$list);
		$template->display('friendlink_list.php');
	}
	
	public function addAction($arg)
	{
		if (isPost())
		{
			$num	= base::init('friendlink')->getCount(array('wid'=>$arg['wid']));
			$arg['sort']	= $num+1;
			base::init('friendlink')->insert($arg);
			$this->refresh('添加成功', "index.php?m=website&a=attribute&wid={$arg['wid']}#tabs-7");
		}
		$template	= $this->template();
		$template->display('friendlink_add.php');
	}
	
	public function editAction($arg)
	{
		if (isPost())
		{
			base::init('friendlink')->update($arg, array('id'=>$arg['id']));
			$this->refresh('修改成功', "index.php?m=website&a=attribute&wid={$arg['wid']}#tabs-7");
		}
		$link		= base::init('friendlink')->fetchRow(array('id'=>$arg['id']));
		$template	= $this->template();
		$template->assign('link',$link);
		$template->display('friendlink_edit.php');
	}
	
	public function deleteAction($arg)
	{
		$id	= !is_array($arg['id']) ? explode(',',$arg['id']) : $arg['id'];
		base::init('friendlink')->delete(array('id'=>$id));
		$this->refresh('删除成功', "index.php?m=website&a=attribute&wid={$arg['wid']}#tabs-7");
	}
	
	public function sortAction($arg)
	{
		$id	= !is_array($arg['id']) ? explode(',',$arg['id']) : $arg['id'];
		foreach ($id as $key=>$val)
		{
			base::init('friendlink')->edit(array('id'=>$val),array('sort'=>$key+1));
		}
		$this->refresh('排序成功', "index.php?m=website&a=attribute&wid={$arg['wid']}#tabs-7");
	}
}